package com.wangan.wanganpush.stomp.controller;

import com.wangan.wanganpush.dao.LoginDao;
import com.wangan.wanganpush.stomp.RelinkMap.LinkMap;
import com.wangan.wanganpush.stomp.sessionHandle.UserLoginHandler;
import com.wangan.wanganpush.stomp.sessionHandle.UserRegHandler;
import com.wangan.wanganpush.utils.AppConstants;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.converter.GenericMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;

import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
import java.util.concurrent.CountDownLatch;

@Controller
public class UserLoginController {

    @Autowired
    LoginDao loginDao;

    private static Logger log = LogManager.getLogger(UserLoginController.class);

    public boolean getUserLoginInfo(){
        boolean handleResult = false;

        final CountDownLatch latch = new CountDownLatch(1);

        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        container.setDefaultMaxTextMessageBufferSize(20*1024*1024);

        WebSocketClient client = new StandardWebSocketClient(container);
        WebSocketStompClient stompClient = new WebSocketStompClient(client);
        stompClient.setInboundMessageSizeLimit(Integer.MAX_VALUE);
        stompClient.setMessageConverter(new GenericMessageConverter());
        final UserLoginHandler sessionHandler = new UserLoginHandler("/queue/nsUserLogin", null,null,latch,loginDao);

        stompClient.connect(AppConstants.STOMP_URL, sessionHandler);

        LinkMap.addStompLink("UserLogin", stompClient);

        return handleResult;
    }

}
